﻿using Medianamik.Core.DAL.SQL;
using NUnit.Framework;

namespace Medianamik.Tests.Unit.Medianamik.Core.DAL.SQL
{
    [TestFixture]
    public class FromClauseTest
    {
        private Table _table1;
        private Table _table2;
        private Table _table3;

        [SetUp]
        public void Init()
        {
            _table1 = new Table("table1", "t1");
            _table1.AddColumn("col1");
            _table1.AddColumn("col2");

            _table2 = new Table("table2", "t2");
            _table2.AddColumn("col1");
            _table2.AddColumn("col2");

            _table3 = new Table("table3", "t3");
            _table3.AddColumn("col1");
            _table3.AddColumn("col2");
        }

        [Test]
        public void SingleTableFromClauseIsProperlyFormatted()
        {
            var from = new FromClause(_table1);

            var expected = " FROM [dbo].[table1] t1";
            Assert.That(from.ToSQL().ToString(), Is.EqualTo(expected));
        }

        [Test]
        public void FromClauseWithSingleInnerJoinIsProperlyFormatted()
        {
            var from = new FromClause(_table1);
            from.InnerJoin(_table1["col1"], _table2["col2"]);

            var join = new InnerJoin(_table1["col1"], _table2["col2"]);

            var expected = " FROM [dbo].[table1] t1" + join.ToSQL();
            Assert.That(from.ToSQL().ToString(), Is.EqualTo(expected));
        }

        [Test]
        public void FromClauseWithMultipleInnerJoinIsProperlyFormatted()
        {
            var from = new FromClause(_table1);
            from.InnerJoin(_table1["col1"], _table2["col2"]);
            from.InnerJoin(_table2["col1"], _table3["col2"]);

            var join1 = new InnerJoin(_table1["col1"], _table2["col2"]);
            var join2 = new InnerJoin(_table2["col1"], _table3["col2"]);

            var expected = " FROM [dbo].[table1] t1" + join1.ToSQL() + join2.ToSQL();
            Assert.That(from.ToSQL().ToString(), Is.EqualTo(expected));
        }
    }
}